package org.budo.graph.repository;

import java.util.Map;

import org.budo.graph.Constant;
import org.budo.graph.entity.AppGraph;
import org.budo.support.lang.util.MapUtil;

import com.alibaba.druid.stat.DruidStatService;
import com.alibaba.druid.support.json.JSONUtils;

import lombok.Getter;
import lombok.Setter;

/**
 * @author lmw
 */
@Getter
@Setter
public abstract class AbstractBudoGraphRepository implements BudoGraphRepository, Constant.Repository {
    /**
     * 暂留, VUE 后移除
     */
    public abstract String readTable(String url);

    public abstract String readTable_json(String url);

    public String readLine(String url, String application) {
        Map<String, String> parameters = DruidStatService.getParameters(url);

        String graphType = MapUtil.get(parameters, PARAMETER_TYPE, AppGraph.GRAPH_TYPE);
        String item = MapUtil.get(parameters, GRAPH_LINE_KEY, "0");

        Map<Double, Object> readLine = this.readLine(graphType, application, item);

        Map<String, Object> zrangeWithScoresToEchartsData = EchartsUtil.toEchartsData(readLine, EchartsUtil.graphTitles(parameters));
        return JSONUtils.toJSONString(zrangeWithScoresToEchartsData);
    }

    public String removeData(String url) {
        Map<String, String> parameters = DruidStatService.getParameters(url);
        String type = parameters.get("Type");

        String host = parameters.get("Host");
        String applicationName = parameters.get("ApplicationName");

        Long from = Long.valueOf(parameters.get("from"));
        Long to = Long.valueOf(parameters.get("to"));

        Long removeData = this.removeData(type, applicationName, host, from, to);
        return "{\"message\":\"cleared " + removeData + " keys\"}";
    }
}